﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.LocLabels
{
    public class Titles
    {
        public List<Title> TitleList;

        public Titles()
        {
            this.TitleList = new List<Title>();
        }

        public Titles(XmlNode node) : this()
        {
            try
            {
                foreach (XmlNode titleXmlNode in node.ChildNodes)
                {
                    Title title = new Title(titleXmlNode);
                    this.TitleList.Add(title);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode locLabelNode)
        {
            try
            {
                XmlNode titlesNode = XmlHelper.AddNode(locLabelNode, "Titles");

                foreach (Title title in this.TitleList)
                {
                    title.AddTitleNode(titlesNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Titles");
                node.Tag = this;
                
                ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Title");
                tsmi.Click += new EventHandler(tsmi_Click);
                ContextMenuStrip cms = new ContextMenuStrip();
                cms.Tag = node;
                cms.Items.Add(tsmi);

                ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);
                
                node.ContextMenuStrip = cms;
                 
                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

            Title title = new Title();
            title.BuildTreeViewElement(selectedNode);

            selectedNode.Expand();

            this.TitleList.Add(title);
        }
    }
}
